using System;
using System.Linq.Expressions;

namespace SuppKids.Core
{
    public static class Lambda
	{
		public static string Property<T>(Expression<Func<T, object>> property)
		{
			var unaryExpression = property.Body as UnaryExpression;
			var memberExpression = (MemberExpression)(unaryExpression != null ? unaryExpression.Operand : property.Body);
			return memberExpression.Member.Name;
		}

		public static string CompositeProperty<T>(Expression<Func<T, object>> property)
		{
			var expr = property.Body.ToString();

			int index = expr.IndexOf('.');

			if (index != -1)
			{
				return expr.Substring(index + 1).TrimEnd(')');
			}

			return expr.TrimEnd(')');
		}
	}
}